package com.vernhe;

import java.util.Comparator;

public class Main {
//    private static class myComparetor1 implements Comparator<Person> {
//        @Override
//        public int compare(Person e1, Person e2) {
//            //实现年龄大的放做边
//            return e1.getAge() - e2.getAge();
//        }
//    }
//    public static class myComparetor2 implements Comparator<Person> {
//        @Override
//        public int compare(Person e1, Person e2) {
//            //实现年龄大的放右边
//            return e2.getAge() - e1.getAge();
//        }
//    }

    static void test1() {
        Integer data[] = new Integer[]{
                7,4,9,2,5,8,11,3,1
        };
//        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
//        for (int i = 0; i < data.length; i++) {
//            bst.add(data[i]);
//        }
        BinarySearchTree<Person> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(new Person(data[i]));
        }
        //默认中序遍历，检查元素的位置是否正确
        //bst.printInfo();
        //测试Visitor
       /* bst.inorder(new BinarySearchTree.Visitor() {
            @Override
            public void visit(Object element) {
                System.out.println("_" + element + "_ ");
            }
        });*/

        //测试toString
        System.out.println(bst);

        //打印高度
        //System.out.println(bst.height());;

        //判断是否为完全二叉树
        System.out.println(bst.isComplete());;
    }

    static void test2() {
        Integer data[] = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };
        BinarySearchTree<Person> bst2 = new BinarySearchTree<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        for (int i = 0; i < data.length; i++) {
            bst2.add(new Person(data[i]));
        }
        bst2.printInfo();
    }

    static void test3() {
        BinarySearchTree<Integer> bst3 = new BinarySearchTree<>();
        for (int i = 0; i < 30; i++) {
            bst3.add((int) (Math.random()*100));
        }
        bst3.printInfo();
    }

    static void test4() {
        //测试删除
        Integer data[] = new Integer[]{
                7,4,9,2,5,8,11,3,12,1
        };
        BinarySearchTree<Integer> bst = new BinarySearchTree<>();
        for (int i = 0; i < data.length; i++) {
            bst.add(data[i]);
        }
        System.out.println(bst);
        bst.remove(7);

        System.out.println(bst);
    }
    public static void main(String[] args) {
        test4();
//        BinarySearchTree<Person> bst = new BinarySearchTree(new myComparetor1());
//        bst.add(new Person(11));
//        bst.add(new Person(15));
    }
}
